package com.gwd.task.services.impl;

import com.gwd.task.componts.Cache;
import com.gwd.task.model.ProductionEventsDTO;
import com.gwd.task.model.TaskDTO;
import com.gwd.task.model.TestDTO;
import com.gwd.task.model.WorkStatisticsDTO;
import com.gwd.task.services.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhiyongming
 * @date 2020/12/31 11:11
 */
@Service
public class WorkStatistServerImpl implements WorkStatistServer {

    @Resource
    private UserGroupServer userGroupServer;
    @Resource
    private TaskServer taskServer;
    @Resource
    private ProductionEventsServer productionEventsServer;
    @Resource
    private TestServer testServer;
    @Resource
    private Cache cache;

    private static final Integer TASK = 0;
    private static final Integer EVENT = 1;
    private static final Integer TEST = 2;

    /**
     * 获取每个人员的各个工作量
     * @param groupCode 组别id
     * @return 统计列表
     */
    @Override
    public List<WorkStatisticsDTO> statist(String groupCode) {
        Map<Long, Long> userMap = new HashMap<>();
        List<WorkStatisticsDTO> list = new ArrayList<>();
        Long groupId = Long.valueOf(groupCode);
        // 获取组别对应的所有员工id
        List<Long> userIds = userGroupServer.findByGroup(groupId);

        // 获取所有任务未完成的
        List<TaskDTO> tasks = taskServer.findByUserIdListAndUnClose(userIds);
        tasks.forEach(task -> {
            if (userMap.get(task.getDevPerson())==null) {
                userMap.put(task.getDevPerson(), task.getTaskId());
                list.add(new WorkStatisticsDTO(task.getDevPerson(), TASK));
            } else {
                list.forEach(d-> {
                    if (d.getUserId().equals(task.getDevPerson())) {
                        d.plusDev();
                    }
                });
            }
        });

        // 获取所有未完成的生产事件
        List<ProductionEventsDTO> events = productionEventsServer.findByUserIdListAndUnClose(userIds);
        events.forEach(event -> {
            if (userMap.get(event.getDevPerson())==null) {
                userMap.put(event.getDevPerson(), event.getDevPerson());
                list.add(new WorkStatisticsDTO(event.getDevPerson(), EVENT));
            } else {
                list.forEach(d -> {
                    if (d.getUserId().equals(event.getDevPerson())) {
                        d.plusEvent();
                    }
                });
            }
        });

        //获取所有未完成的测试
        List<TestDTO> tests = testServer.findByUserIdListAndUnClose(userIds);
        tests.forEach(test -> {
            if (userMap.get(test.getDevPerson())==null) {
                userMap.put(test.getDevPerson(), test.getDevPerson());
                list.add(new WorkStatisticsDTO(test.getDevPerson(), TEST));
            } else {
                list.forEach(d -> {
                    if (d.getUserId().equals(test.getDevPerson())) {
                        if (test.getType()==0) {
                            d.plusTest();
                        } else {
                            if (test.getStatus()==8) {
                                d.plusSelfTest();
                            } else {
                                d.plusBack();
                            }
                        }
                    }
                });
            }
        });

        cache.matchUserName(list);
        list.forEach(WorkStatisticsDTO::computeTotal);
        return list;
    }
}
